package com.vic.Builder;

import com.vic.bean.constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Vic Ly
 */
public class builderBase {
    private static Logger log = LoggerFactory.getLogger(builderBase.class);
    // 生成一些辅助类
    public static void execute(){
        List<String> headInfos = new ArrayList();
        // 生成日期格式枚举
        headInfos.add("package "+constant.ENUMS_PACKAGE);
        build(headInfos,"DateTimePatternEnum", constant.ENUMS_PATH);
        // clear()这一步必须做
        // 这里使用的同一个List,如果不清空，除第一个生成类外,其余生成的所有类中的"package XXX"字样会重叠
        headInfos.clear();
        // 生成日期工具类
        headInfos.add("package "+constant.UTILS_PACKAGE);
        build(headInfos,"DateUtils", constant.UTILS_PATH);

        // 生成PaginationResultVO
        headInfos.clear();
        headInfos.add("package "+constant.VO_PACKAGE);
        build(headInfos,"PaginationResultVo", constant.VO_PATH);

        // 生成BaseParam
        headInfos.clear();
        headInfos.add("package "+constant.GLOBAL_QUERY_PACKAGE);
        headInfos.add("import "+constant.OTHER_PACKAGE+".SimplePage;");
        build(headInfos,"BaseQuery", constant.GLOBAL_QUERY_PATH);

        // 生成SimplePage
        headInfos.clear();
        headInfos.add("package "+constant.OTHER_PACKAGE);
        headInfos.add("import "+constant.ENUMS_PACKAGE+".PageSizeEnum;");
        build(headInfos,"SimplePage", constant.OTHER_PATH);

        // 生成PageSizeEnum
        headInfos.clear();
        headInfos.add("package "+constant.ENUMS_PACKAGE);
        build(headInfos,"PageSizeEnum", constant.ENUMS_PATH);

        // 生成ResponseCodeEnum
        headInfos.clear();
        headInfos.add("package "+constant.ENUMS_PACKAGE);
        build(headInfos,"ResponseCodeEnum", constant.ENUMS_PATH);

        // 生成BusinessException
        headInfos.clear();
        headInfos.add("package "+constant.EXCEPTION_PACKAGE);
        headInfos.add("import "+constant.ENUMS_PACKAGE+".ResponseCodeEnum;");
        build(headInfos,"BusinessException", constant.EXCEPTION_PATH);

        // 生成BaseController
        headInfos.clear();
        headInfos.add("package "+constant.GLOBAL_CONTROLLER_PACKAGE);
        headInfos.add("import "+constant.VO_PACKAGE+".ResponseVO;");
        headInfos.add("import "+constant.ENUMS_PACKAGE+".ResponseCodeEnum;");
        build(headInfos,"BaseController", constant.GLOBAL_CONTROLLER_PATH);

        // ResponseVO
        headInfos.clear();
        headInfos.add("package "+constant.VO_PACKAGE);
        build(headInfos,"ResponseVO", constant.VO_PATH);

        // GlobalExceptionController
        headInfos.clear();
        headInfos.add("package "+constant.GLOBAL_CONTROLLER_PACKAGE);
        headInfos.add("import "+constant.VO_PACKAGE+".ResponseVO;");
        headInfos.add("import "+constant.ENUMS_PACKAGE+".ResponseCodeEnum;");
        headInfos.add("import "+constant.EXCEPTION_PACKAGE+".BusinessException;");
        build(headInfos,"GlobalExceptionController", constant.GLOBAL_CONTROLLER_PATH);


        // 生成BaseMapper
        headInfos.clear();
        headInfos.add("package "+constant.MAPPER_BASE_PACKAGE);
        headInfos.add("import "+constant.VO_PACKAGE+".ResponseVO;");
        headInfos.add("import "+constant.ENUMS_PACKAGE+".ResponseCodeEnum;");
        build(headInfos,"BaseMapper", constant.MAPPER_BASE_PATH);


    }
    // 读文本文件并生成
    // (工具类或者枚举类等一般写法固定，所以直接将代码放在txt直接读出再生成即可)
    private static void build(List<String> headInfos,String fileName, String outPutPath){
        File folder = new File(outPutPath);
        if(!folder.exists()){
            folder.mkdirs();
        }
        File javaFile = new File(outPutPath,fileName + ".java");
        OutputStream out = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;

        InputStream in = null;
        InputStreamReader isr = null;
        BufferedReader  br = null;

        try{
           out = new FileOutputStream(javaFile);
           osw = new OutputStreamWriter(out,"utf-8");
           bw = new BufferedWriter(osw);

           String tempPath = builderBase.class.getClassLoader().getResource("template/" + fileName + ".txt").getPath();
           in = new FileInputStream(tempPath);
           isr = new InputStreamReader(in,"utf-8");
           br = new BufferedReader(isr);

           for (String head: headInfos){
               bw.write(head + ";");
               bw.newLine();
               bw.newLine();
           }

           String lineInfo = null;
           while ((lineInfo = br.readLine())!= null){
               bw.write(lineInfo);
               bw.newLine();
           }
           bw.flush();
        }catch (Exception e){
            log.error("生成基础类:{}失败,原因:",fileName,e);
        }finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(isr != null){
                try {
                    isr.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(osw != null){
                try {
                    osw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
